<template>
  <div>
    <i-dialog
      :title="dialogType === 'BatchAdd' ? '批量添加' : '批量删除'"
      :visible="visible"
      width="642px"
      :close-on-click-modal="false"
      @close="outerClose"
      @open="outerOpen"
    >
      <div>
        <LoadFile v-model="uploadData" ref="loadFile" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outerClose">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </span>
    </i-dialog>
  </div>
</template>
<script>
import LoadFile from '@/components/LoadFile'
import { batchAddprofitShare, batchDelprofitShare } from '@/api/divideDevice'
import { exportExcel } from '@/utils'
export default {
  name: 'BatchUpload',
  components: { LoadFile },
  data() {
    return {
      uploadData: [],
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    dialogType: {
      type: String,
      default: 'BatchAdd',
    },
  },
  methods: {
    refreshTable() {
      this.$emit('refreshTable')
    },
    outerClose() {
      this.$emit('update:visible', false)
    },
    outerOpen() {},
    validElementDeviceIdLength(params) {
      for (var i = 0, len = params.length; i < len; i++) {
        if (params[i].deviceId.length > 32) {
          this.$message('数据错误,请检查导入的数据!设备号输入错误')
          return false
        }
      }
      return true
    },

    validElement(params) {
      for (var i = 0; i < params.length; i++) {
        if (params[i].deviceId.length > 32) {
          this.$message('数据错误,请检查导入的数据!设备号输入错误')
          return false
        }

        if (
          params[i].splitValue > 30 ||
          params[i].splitValue < 1 ||
          !/^[1-9]\d*$/.test(params[i].splitValue)
        ) {
          this.$message('数据错误,请检查导入的数据!分账比例输入错误！')
          return false
        }

        if (!params[i].deviceId || !params[i].splitValue) {
          this.$message('数据错误,请检查导入的数据!设备与分账比例，需对应！')
          return false
        }
      }
      return true
    },

    handleBatchAdd() {
      let profitShareList = []
      console.log(this.uploadData)
      this.uploadData.forEach((item) => {
        let itemData = {}
        item.DeviceID && (itemData.deviceId = item.DeviceID)
        item.SplitValue &&
          (itemData.splitValue =
            typeof item.SplitValue === 'string'
              ? parseFloat(item.SplitValue.trim())
              : parseFloat(item.SplitValue))
        profitShareList.push(itemData)
      })

      if (!this.validElement(profitShareList)) {
        this.$refs.loadFile.onRemove()
        return
      }
      batchAddprofitShare({
        profitShareList: profitShareList,
      }).then(
        (res) => {
          exportExcel(res, '添加分账配置导入失败表')
          this.outerClose()
          this.refreshTable()
        },
        () => {
          this.outerClose()
          this.refreshTable()
        }
      )
    },
    handleBatchDelete() {
      let profitShareList = []
      this.uploadData.forEach((item) => {
        let itemData = {}
        item.DeviceID && (itemData.deviceId = item.DeviceID)
        profitShareList.push(itemData)
      })
      if (!this.validElementDeviceIdLength(profitShareList)) {
        this.$refs.loadFile.onRemove()
        return
      }

      batchDelprofitShare({
        profitShareList: profitShareList,
      }).then(
        (res) => {
          exportExcel(res, '删除分账配置导入失败表')
          this.outerClose()
          this.refreshTable()
        },
        () => {
          this.outerClose()
          this.refreshTable()
        }
      )
    },

    submit() {
      switch (this.dialogType) {
        case 'BatchAdd':
          this.handleBatchAdd()
          break
        case 'BatchDelete':
          this.handleBatchDelete()
          break
        default:
          break
      }
    },
  },
}
</script>
